package com.daman.db
import xitrum.annotation.GET
import xitrum.Action
import java.sql.{Connection, ResultSet}
@GET("/db")
class DbTest extends Action {

  def execute() {
    val conn = DBToolkit.getConn()
    val  out = this.readMap(conn)
   // val out = this.insert(conn)
    // val out = this.readObj(conn)
    //Main.delete(conn)
    // val out = this.deleteAsBatch(conn)
    DBToolkit.closeConn(conn)
    respondJson(out)
  }

  def insert(conn:Connection) = {
    val sql = "insert into  dashop_product_category (name) values (?) "
    val params = List("insert")
    DBToolkit.execute(conn,sql,params)
  }

  def readMap(conn:Connection) = {
    val sql = "select * from  dashop_product_category "
    val params = List()
    DBToolkit.readMap(conn,sql,params)
  }

  def delete(conn:Connection) ={
    val sql = "delete from dashop_product_category where id = ? "
    val params = List("1")
    DBToolkit.execute(conn,sql,params)
  }

  def deleteAsBatch(conn:Connection) ={
    val sql = "delete from dashop_product_category where id = ? "
    val params = List(List("1"),List("2"))
    DBToolkit.executeAsBatch(conn,sql,params)
  }

  def readObj(conn:Connection) = {
    val sql = "select id from dashop_product_category where name= ? "
    val params = List("haha")
    val out = DBToolkit.readObj(conn,sql,params,(resultSet:ResultSet) => {
      resultSet.getString(1)
    }  )
    out
  }



}
